package org.wlw.teacher.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
import org.wlw.teacher.context.SystemContext;
import org.wlw.teacher.models.BaseModel;
import org.wlw.teacher.models.Pager;

//@Component
//@Aspect
public class PagerAspect {

    //    @Pointcut("execution(* org.wlw.teacher.service.impl.*.find(..))")
    public void pc1() {
    }

    //    @Before("pc1()")
    public Object setCondition(ProceedingJoinPoint joinPoint) {
        try {
            //开始加条件
            Object[] objs = joinPoint.getArgs();
            BaseModel bm = (BaseModel) objs[0];
            if (bm == null) bm = new BaseModel();
            int pageSize = SystemContext.getPageSize();
            int pageIndex = SystemContext.getPageIndex();
            int pageStart = (pageIndex - 1) * pageSize;
            bm.setPageSize(pageSize);
            bm.setPageStart(pageStart);

            //执行方法得到返回值
            Object rel = joinPoint.proceed();

            //添加返回信息
            Pager pager = (Pager) rel;
            int totalPage = ((pager.getTotals() - 1) / pageSize) + 1;
//            System.out.println(pager.getData());
            pager.setPageIndex(pageIndex);
            pager.setPageTotal(totalPage);
            pager.setPageSize(pageSize);
//            System.out.println(pager.getPageSize());
            return rel;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
